package org.framework.edge.builder.application.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.framework.edge.builder.application.DockerRepositoryApplication;
import org.framework.edge.builder.application.assembler.DockerRepositoryDTOAssembler;
import org.framework.edge.builder.application.command.docker.repository.*;
import org.framework.edge.builder.application.dto.DockerRepositoryDTO;
import org.framework.edge.builder.docker.DockerRuntime;
import org.framework.edge.builder.domain.model.docker.repository.DockerRepository;
import org.framework.edge.builder.domain.model.docker.repository.DockerRepositoryRepository;
import org.framework.edge.builder.domain.model.docker.images.DockerImages;
import org.framework.edge.builder.domain.model.docker.images.DockerImagesRepository;
import org.wu.framework.core.exception.RuntimeExceptionFactory;
import org.wu.framework.lazy.orm.web.plus.stereotype.LazyApplication;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * describe Docker 仓库 
 *
 * @author Jia wei Wu
 * @date 2024/08/13 07:39 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@Slf4j
@LazyApplication
public class DockerRepositoryApplicationImpl implements DockerRepositoryApplication {

    @Resource
    DockerRepositoryRepository dockerRepositoryRepository;

    @Resource
    DockerRuntime dockerRuntime;



    /**
     * 仓库登录
     *
     * @param dockerRepository 仓库信息
     * @return 仓库信息
     */
    @Override
    public Result<?> login(DockerRepository dockerRepository) {
        // 登录测试
        String url = dockerRepository.getUrl();
        String username = dockerRepository.getUsername();
        String password = dockerRepository.getPassword();
        try {
            Result<?> loginResult = dockerRuntime.loginRepository(url, username, password);
            if (!loginResult.isSuccess()) {
                return ResultFactory.errorOf(loginResult);
            }
            return ResultFactory.successOf();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultFactory.errorOf(e);
        }
    }

    /**
     * describe 新增Docker 仓库
     *
     * @param dockerRepositoryStoryCommand 新增Docker 仓库     
     * @return {@link Result<DockerRepository>} Docker 仓库新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepository> story(DockerRepositoryStoryCommand dockerRepositoryStoryCommand) {
        // 登录测试
        DockerRepository dockerRepository = DockerRepositoryDTOAssembler.INSTANCE.toDockerRepository(dockerRepositoryStoryCommand);
        Result<?> login = login(dockerRepository);
        if (login.isSuccess()) {
            return dockerRepositoryRepository.story(dockerRepository);
        } else {
            return ResultFactory.errorOf(login);
        }
    }
    /**
     * describe 批量新增Docker 仓库
     *
     * @param dockerRepositoryStoryCommandList 批量新增Docker 仓库     
     * @return {@link Result<List<DockerRepository>>} Docker 仓库新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<List<DockerRepository>> batchStory(List<DockerRepositoryStoryCommand> dockerRepositoryStoryCommandList) {
        List<DockerRepository> dockerRepositoryList = dockerRepositoryStoryCommandList.stream().map( DockerRepositoryDTOAssembler.INSTANCE::toDockerRepository).collect(Collectors.toList());
        for (DockerRepository dockerRepository : dockerRepositoryList) {
            // 登录测试
            Result<?> login = login(dockerRepository);
            if (!login.isSuccess()) {
                dockerRepositoryList.remove(dockerRepository);
            }
        }
        return dockerRepositoryRepository.batchStory(dockerRepositoryList);
    }
    /**
     * describe 更新Docker 仓库
     *
     * @param dockerRepositoryUpdateCommand 更新Docker 仓库     
     * @return {@link Result<DockerRepository>} Docker 仓库领域对象     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepository> updateOne(DockerRepositoryUpdateCommand dockerRepositoryUpdateCommand) {
        DockerRepository dockerRepository = DockerRepositoryDTOAssembler.INSTANCE.toDockerRepository(dockerRepositoryUpdateCommand);
        Result<?> login = login(dockerRepository);
        if (login.isSuccess()) {
            return dockerRepositoryRepository.story(dockerRepository);
        } else {
            return ResultFactory.errorOf(login);
        }
    }



    /**
     * describe 查询单个Docker 仓库
     *
     * @param dockerRepositoryQueryOneCommand 查询单个Docker 仓库     
     * @return {@link Result<DockerRepositoryDTO>} Docker 仓库DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepositoryDTO> findOne(DockerRepositoryQueryOneCommand dockerRepositoryQueryOneCommand) {
        DockerRepository dockerRepository = DockerRepositoryDTOAssembler.INSTANCE.toDockerRepository(dockerRepositoryQueryOneCommand);
        return dockerRepositoryRepository.findOne(dockerRepository).convert(DockerRepositoryDTOAssembler.INSTANCE::fromDockerRepository);
    }

    /**
     * describe 查询多个Docker 仓库
     *
     * @param dockerRepositoryQueryListCommand 查询多个Docker 仓库     
     * @return {@link Result<List<DockerRepositoryDTO>>} Docker 仓库DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<List<DockerRepositoryDTO>> findList(DockerRepositoryQueryListCommand dockerRepositoryQueryListCommand) {
        DockerRepository dockerRepository = DockerRepositoryDTOAssembler.INSTANCE.toDockerRepository(dockerRepositoryQueryListCommand);
        return dockerRepositoryRepository.findList(dockerRepository)        .convert(dockerRepositorys -> dockerRepositorys.stream().map(DockerRepositoryDTOAssembler.INSTANCE::fromDockerRepository).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个Docker 仓库
     *
     * @param dockerRepositoryQueryListCommand 分页查询多个Docker 仓库     
     * @return {@link Result<LazyPage<DockerRepositoryDTO>>} 分页Docker 仓库DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<LazyPage<DockerRepositoryDTO>> findPage(int size,int current,DockerRepositoryQueryListCommand dockerRepositoryQueryListCommand) {
        DockerRepository dockerRepository = DockerRepositoryDTOAssembler.INSTANCE.toDockerRepository(dockerRepositoryQueryListCommand);
        return dockerRepositoryRepository.findPage(size,current,dockerRepository)        .convert(page -> page.convert(DockerRepositoryDTOAssembler.INSTANCE::fromDockerRepository))            ;
    }

    /**
     * describe 删除Docker 仓库
     *
     * @param dockerRepositoryRemoveCommand 删除Docker 仓库     
     * @return {@link Result<DockerRepository>} Docker 仓库     
     
     * @author Jia wei Wu
     * @date 2024/08/13 07:39 晚上
     **/

    @Override
    public Result<DockerRepository> remove(DockerRepositoryRemoveCommand dockerRepositoryRemoveCommand) {
     DockerRepository dockerRepository = DockerRepositoryDTOAssembler.INSTANCE.toDockerRepository(dockerRepositoryRemoveCommand);
     return dockerRepositoryRepository.remove(dockerRepository);
    }

}